package org.example.filter;

import com.google.common.collect.Sets;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter;
import org.springframework.boot.autoconfigure.AutoConfigurationMetadata;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Set;

@Component
public class CustomAutoConfigurationFilter implements AutoConfigurationImportFilter {
    private static final Set<String> excludeAutoConfigurationClassesSet = Sets.newHashSet();
    @Value("${spring.kafka.enable:false}")
    private Boolean kafkaEnable;
    @Value("${spring.redis.enable:false}")
    private Boolean redisEnable;
    @Value("${spring.rabbitmq.enable:false}")
    private Boolean rabbitEnable;
    @Value("${mqtt.enable:false}")
    private Boolean mqttEnable;
    @Value("${spring.cloud.openfeign.enabled:false}")
    private Boolean feignEnable;
    @Value("${resilience4j.enable:false}")
    private Boolean resilience4jEnable;
    @Value("${spring.cloud.nacos.enabled:false}")
    private Boolean nacosEnable;
    @Value("${shiro.enable:false}")
    private Boolean shiroEnable;

    @PostConstruct
    public void init() {
        addAutoConfigurationClasses("org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration", kafkaEnable);
        addAutoConfigurationClasses("org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration", redisEnable);
        addAutoConfigurationClasses("org.redisson.spring.starter.RedissonAutoConfiguration", redisEnable);
        addAutoConfigurationClasses("org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration", rabbitEnable);
        addAutoConfigurationClasses("org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration", mqttEnable);
        addAutoConfigurationClasses("org.springframework.cloud.openfeign.FeignAutoConfiguration", feignEnable);
        addAutoConfigurationClasses("io.github.resilience4j.springboot.autoconfigure.Resilience4JCircuitBreakerAutoConfiguration", resilience4jEnable);
        addAutoConfigurationClasses("com.alibaba.cloud.nacos.NacosDiscoveryAutoConfiguration", nacosEnable);
        addAutoConfigurationClasses("com.alibaba.cloud.nacos.NacosConfigAutoConfiguration", nacosEnable);
        addAutoConfigurationClasses("org.apache.shiro.spring.config.ShiroAnnotationProcessorAutoConfiguration", shiroEnable);
        addAutoConfigurationClasses("org.apache.shiro.spring.boot.autoconfigure.ShiroAutoConfiguration", shiroEnable);
        addAutoConfigurationClasses("org.apache.shiro.spring.config.web.autoconfigure.ShiroWebAutoConfiguration", shiroEnable);
    }

    private void addAutoConfigurationClasses(String autoConfigurationClass, Boolean enable) {
        if (BooleanUtils.isNotTrue(enable)) {
            excludeAutoConfigurationClassesSet.add(autoConfigurationClass);
        }
    }

    @Override
    public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) {
        boolean[] result = new boolean[autoConfigurationClasses.length];
        for (int i = 0; i < autoConfigurationClasses.length; i++) {
            if (excludeAutoConfigurationClassesSet.contains(autoConfigurationClasses[i])) {
                result[i] = false;// 排除
            } else {
                result[i] = true;
            }
        }
        return result;
    }
}
